global.database_name = global.APPNAME || "MyApp";
var mon_db = require("./mongodb.index.js");
var db_core //数据库内核

var db_type = {
	get: function() {
		if (fs.existsSync(__dirname + "/.db/.db_type")) {
			return fs.readFileSync(__dirname + "/.db/.db_type").toString();
		} else {
			return "";
		}
	},
	set: function(content) {
		fs.writeFileSync(__dirname + "/.db/.db_type", content);
	}
};
//检查数据库支持
if (db_type.get() !== "file" && mon_db._isConnectAble()) {
	console.log("启用Mongodb数据库");
	db_core = mon_db;
} else {
	//折衷使用文件数据库
	console.log("启用Filedb数据库");
	db_core = require("./file.index.js");
}
db_core.refreshIndex();
var exportsDbCore = {};
exportsDbCore.__proto__ = db_core;
exportsDbCore.changeCore = function(type) {
	console.log("变更数据库核心");
	if (type == "file") {
		db_type.set("file");
		exportsDbCore.__proto__ = require("./file.index.js");
	} else {
		db_type.set("mongodb");
		exportsDbCore.__proto__ = mon_db;
	}
}

module.exports = exportsDbCore;